import { _decorator } from 'cc';
import { DIRECTION_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PLAYER_DEAD_TYPE } from '../../Enum';
import { TILE_WEIGHT, TILE_HEIGHT } from '../Tile/TileItem';
import { EntiyManager } from '../../common/EnityManager';
import { WoodenSkeletonStateMachine } from './WoodenSkeletonStateMachine';
import { EventManager } from '../../RunTime/EventManager';
import { DataManager } from '../../RunTime/DataManager';
import { EnemyManager } from '../../common/EnemyManager';
import { IEntity } from '../../Levels';
const { ccclass, property } = _decorator;


@ccclass('WoodenSkeletonManager')
export class WoodenSkeletonManager extends EnemyManager {

  id: number
  // 移动使用
  targetX: number = 0
  targetY: number = 0
  readonly speed: number = 0.1
  in_moving: boolean = false


  async init(param: IEntity){
    this.fsm = this.addComponent(WoodenSkeletonStateMachine)
    await this.fsm.init()
    super.init(param)

    EventManager.getInstance().on(EVENT_ENUM.PLAYER_MOVE_END, this.OnAttack, this)

  }

  onDestroy(): void {
    super.onDestroy()
    EventManager.getInstance().off(EVENT_ENUM.PLAYER_MOVE_END, this.OnAttack)
  }

  OnAttack(){
    if (this.state == ENTITY_STATE_ENUM.DEAD) {
      return
    }
    const player = DataManager.getInstance().player
    const {x, y} = player
    const disX = Math.abs(this.x - x)
    const disY = Math.abs(this.y - y)
    if ( ((disX <= 1 && disY === 0) || (disY <= 1 && disX === 0)) && !player.isInState(ENTITY_STATE_ENUM.DEAD) && !player.isInState(ENTITY_STATE_ENUM.AIRDEAD) ){
      this.state = ENTITY_STATE_ENUM.ATTACK
      EventManager.getInstance().emit(EVENT_ENUM.PLAYER_DIE, PLAYER_DEAD_TYPE.ENEMY_ATTACK)
    }
    else {
      this.state = ENTITY_STATE_ENUM.IDLE
    }


  }




}
